home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / Simple Print ƒ / Simple Print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-27  |  6.9 KB  |  224 lines  |  [TEXT/KAHL]

  1. //---------------------------------------------------------------------------
  2. //
  3. //    SimplePrint
  4. //
  5. //    SimplePrint makes the minimum calls required to print one page under the
  6. //    "new" Print Manager. It will print 85 rectangles that are scaled,
  7. //    rotated, and framed. The gxColor of the rectangle frmae will be a color derived 
  8. //    from the HSV color space. Each rectangle will contain a slightly different
  9. //     HSV color.
  10. //
  11. //    By the way, if an error occurs, you will end up in Macsbug with an 
  12. //    appropriate error message.
  13. //
  14. //
  15. //    History:    
  16. //                6/91 - New
  17. //                8/93 - convert files to work with the ß2 "GXified" interface files
  18. //
  19. // ©1991 - 1993 Apple Computer, Inc.  All rights reserved.
  20. //
  21. //---------------------------------------------------------------------------
  22.  
  23.  
  24. #include <Types.h>            // defines noErr
  25. #include <Errors.h>
  26. #include <QuickDraw.h>
  27. #include <Fonts.h>
  28. #include <Dialogs.h>
  29. #include <Memory.h>
  30.  
  31. #include <Collections.h>
  32. #include <GXPrinting.h>
  33. #include <GXGraphics.h>
  34. #include <GraphicsLibraries.h>
  35. #include <GXMath.h>
  36.  
  37. #define f(a,b)    (((Fixed) (a) << 16) + (b))
  38.  
  39. #define kDebugging    false        // Set to TRUE, for error & notice posting.
  40.  
  41.  
  42. void PrintOne(gxShape thePage);
  43. void DrawPage(gxShape thePage);
  44.  
  45.  
  46. //------  PrintOne  ---------------------------------------------------------
  47. //
  48. //    Print the contents of "thePage".  This function contains the minimum 
  49. //    number of calls required to print a page under the "new" Print Manager.
  50. //
  51. void PrintOne(gxShape thePage)
  52. {
  53.     OSErr     anErr;
  54.     gxJob    theJob;
  55.  
  56.     // We allocate space for a new job which contains a default format and
  57.     //    paper type.
  58.     anErr = GXNewJob(&theJob);
  59.     
  60.     if (anErr != noErr) DebugStr("\p Blam! GXNewJob");
  61.     
  62.         // Display the extensible Print dialog box
  63.         if (GXJobPrintDialog(theJob, nil) == gxOKSelected)
  64.             {
  65.                 // Start sending the job to a spool file. The job will be 
  66.                 // named: "Spinning Rectangles Document", and it contains 1 page
  67.                 // The name will be displayed in he status dialogs.
  68.                 GXStartJob(theJob, "\pSpinning Rectangles Document", 1);
  69.                 
  70.                 if (GXGetJobError(theJob) != noErr) DebugStr("\p Blam! GXStartJob");
  71.             
  72.                     // Send the entire page down to the printer.  We can send the entire
  73.                     // job because all of the shapes that are being printed have been
  74.                     // collected into "thePage", therefore the Print Manager does not
  75.                     // need to do any work.
  76.                     GXPrintPage(theJob, 1, GXGetJobFormat(theJob, 1), thePage);
  77.                     if (GXGetJobError(theJob) != noErr) DebugStr("\p Blam! GXPrintPage");
  78.             
  79.                 // This calls tells the Pritn Manager that we have finished sending the spool 
  80.                 // file, therefore terminate the transmission (i.e. the connection to the
  81.                 // printer.
  82.                 GXFinishJob(theJob);
  83.                 if (GXGetJobError(theJob) != noErr) DebugStr("\p Blam! GXFinishJob");
  84.             }
  85.     
  86.     anErr = GXDisposeJob(theJob);
  87.     
  88.     if (anErr != noErr) DebugStr("\p Blam! GXDisposeJob");
  89. }
  90.  
  91.  
  92.  
  93. //------  DrawPage  ---------------------------------------------------------
  94. //
  95. // In this function, we collect the 85 rectangles that are scaled, rotated, and 
  96. //    framed into the gxShape called "thePage". Each gxRectangle will be drawn with a 
  97. //    different HSV gxColor.  All of the rectangles will be collected into "thePage", 
  98. //    which will be printed via the PrintOne function.
  99. //
  100. void DrawPage(gxShape thePage)
  101. {
  102.     gxShape         myRect;
  103.     gxRectangle     rectangleBounds, myRectDimensions = {ff(0), ff(0), ff(300), ff(300)};
  104.     short             drawLoop;
  105.     gxColor         frameColor;
  106.     Fixed             x, y;
  107.  
  108.     // Create the gxRectangle to be drawn.
  109.        myRect = GXNewRectangle(&myRectDimensions);
  110.  
  111.     // Set up the HSV gxColor space, which will allow us to change the gxColor
  112.     // of the framed gxRectangle. As we scale and rotate it 85 times.
  113.     frameColor.space                     = gxHSVSpace;
  114.     frameColor.profile                     = nil;
  115.     frameColor.element.hsv.hue             = 0x0000;
  116.     frameColor.element.hsv.saturation     = 0xFFFF;
  117.     frameColor.element.hsv.value         = 0x7FFF;
  118.  
  119.  
  120.      // Set up the various components of the gxRectangle.
  121.      GXSetShapeFill (myRect, gxClosedFrameFill);
  122.     GXSetShapePen(myRect, 0);
  123.      GXSetShapeColor(myRect, &frameColor);
  124.     
  125.     
  126.     // Move the gxRectangle to the center of the page, that will eventually be printed
  127.     GXMoveShapeTo (myRect,  ff(150), ff(150));  
  128.  
  129.  
  130.     // Add the 85 rectangles to "thePage". Each gxRectangle will be scaled, rotated,
  131.     // and framed in an HSV gxColor.
  132.     
  133.     for (drawLoop = 0; drawLoop < 85; drawLoop++)
  134.     {
  135.         gxShape        tempShape;
  136.     
  137.         // The first time through the loop, we add an unrotated gxRectangle to the 
  138.         //    "thePage".
  139.         AddToShape(thePage, myRect);
  140.         
  141.         // Make a temporary copy of the gxRectangle to "tempShape". If we did not make
  142.         //    a copy of "myRect", we would add the same gxShape to the picture over and over.
  143.         // The graphics system assumes that the gxShape has not changed, unless the
  144.         //    tranform of the gxShape or the gxShape was re-created, since the last time the 
  145.         //    gxShape was drawn or added to the picture.
  146.         tempShape = GXCopyToShape (nil, myRect);
  147.         
  148.         // We decrement the owner count to 1, by calling GXDisposeShape, so that "thePage"
  149.         //    is the only owner.  Whcih enables the graphics system to realize that the
  150.         //    gxShape has changed since, the last time it had been drawn.
  151.         GXDisposeShape (myRect);
  152.         
  153.         // Re-create the gxShape
  154.         myRect = tempShape;
  155.         
  156.         // Get the bounds of the gxRectangle.    
  157.         GXGetShapeBounds(myRect, 0L, &rectangleBounds);
  158.         
  159.         // Divide the hieght and width of the gxRectangle bounds by 2
  160.         x = rectangleBounds.left + rectangleBounds.right >> 1;
  161.         y = rectangleBounds.top + rectangleBounds.bottom >> 1;
  162.  
  163.         // Scale and rotate the gxRectangle.
  164.         GXScaleShape(myRect, f(0, 0xF800), f(0, 0xF800), x, y);
  165.         GXRotateShape(myRect, ff(25), x, y);          
  166.         
  167.         // Change the gxColor of the gxRectangle frame, and set the new gxColor to the gxRectangle.    
  168.         frameColor.element.hsv.hue += 0x0300;
  169.           GXSetShapeColor(myRect, &frameColor);
  170.     }
  171.  
  172.     GXDisposeShape (myRect);
  173. }
  174.  
  175.  
  176.  
  177. //------  main  -------------------------------------------------------------
  178.  
  179. main()
  180. {
  181.     OSErr    anErr;
  182.     
  183.     MaxApplZone(); MoreMasters(); MoreMasters();
  184.     
  185.     InitGraf((Ptr) &qd.thePort);
  186.     InitFonts();
  187.     InitWindows();
  188.     InitDialogs(nil);
  189.     InitCursor();
  190.     
  191.     if (kDebugging) {
  192.         SetGraphicsLibraryErrors();
  193.         SetGraphicsLibraryNotices();
  194.     }
  195.     
  196.     // Standard initilization to get the graphics system set up. We also gxInitialize
  197.     //    the error and notice functions of the system to allow us to be alerted when
  198.     // an error occurs or when we do something that does not make sense to the 
  199.     //    graphics system.
  200.     GXEnterGraphics();
  201.     
  202.     
  203.     // Initialize the "new" Print Manager
  204.     anErr = GXInitPrinting();
  205.     if (anErr != noErr) DebugStr("\p Blam! GXInitPrinting");
  206.     {
  207.         gxShape thePage;
  208.     
  209.         // Create "thePage" gxShape, and fill it full of rectangles by calling DrawPage ()
  210.         thePage = GXNewShape(gxPictureType);
  211.         DrawPage(thePage);
  212.     
  213.         // Print "thePage" and dispose of it.
  214.         PrintOne(thePage);
  215.         GXDisposeShape(thePage);
  216.     }
  217.     
  218.     // Close up the "new" Print Manager
  219.     anErr = GXExitPrinting();
  220.     if (anErr != noErr) DebugStr("\p Blam! GXExitPrinting");
  221.  
  222.     // Close up the graphics system
  223.     GXExitGraphics();
  224. }